﻿using System;
using System.Collections.Generic;

using DigitalQuran.Data.Metadata;

namespace DigitalQuran.Collection
{
    /// <summary>
    /// Collection of <see cref="DigitalQuran.Page"/>
    /// </summary>
    public class PageCollection : ReadOnlyCollection<Page>
    {
        /// <summary>
        /// Total Pages in Qur'an
        /// </summary>
        public const int TotalPages = 604;

        internal PageCollection() : this(0, TotalPages) { }

        int startIndex = 0;
        /// <summary>
        /// Creats a subset of <see cref="DigitalQuran.Collection.PageCollection"/>
        /// </summary>
        /// <param name="startIndex">First Page Index</param>
        /// <param name="count">Total Pages to Include in Collection</param>
        public PageCollection(int startIndex, int count)
            : base(count)
        {
            this.startIndex = startIndex;
        }

        public override Page this[int index]
        {
            get
            {
                if (index < Count)
                    return _Pages[index + startIndex];
                else
                    throw new IndexOutOfRangeException();
            }
        }

        public override IEnumerator<Page> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return _Pages[i + startIndex];
            }
        }

        static Page[] _Pages = new Page[TotalPages];
        internal static void LoadData(IEnumerator<ValueSpan> e)
        {
            for (int i = 0; i < TotalPages; i++)
            {
                e.MoveNext();
                _Pages[i] = new Page(i, e.Current.LowerBound, e.Current.Count);
            }
        }
    }
}